﻿using EventBus;
using LiteStep.Currencies;
using LiteStep.DataSources;
using LiteStep.Events;
using LiteStep.Events.Handlers;
using LiteStep.Exchanges;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiteStep.Conversions.ConversionUpdaters
{
	public abstract class BasicTickerConversionUpdater<TFrom, TTo, TOn, TTicker> : 
		IConversionUpdater<TFrom, TTo, TOn>,
		IAfterTickerResponseEventHandler<TTicker>
		where TFrom : ICurrency
		where TTo : ICurrency
		where TOn : IExchange
		where TTicker : ITicker
	{
		protected IConversionManager conversionManager { get; set; }
		protected IEventBus eventBus { get; set; }
		private IHandlerRegistration registration { get; set; }

		public BasicTickerConversionUpdater(IConversionManager conversionManager, IEventBus eventBus)
		{
			this.conversionManager = conversionManager;
			this.eventBus = eventBus;
		}

		public void Register()
		{
			if (registration == null)
			{
				registration = eventBus.AddHandler(AfterTickerResponseEvent<TTicker>.TYPE, this);
			}
		}

		public void Unregister()
		{
			if (registration != null)
			{
				registration.RemoveHandler();
				registration = null;
			}
		}

		public abstract void OnAfterTickerResponseEvent(AfterTickerResponseEvent<TTicker> e);
	}
}
